<template>
  <div class="goods-list">
    <div class="goods-item" v-for="item in goodsList" :key="item.id" @click="goDetail(item.id)">
      <img :src="item.url" alt />
      <h1 class="title">{{item.title}}---{{item.id}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{item.newPrice}}</span>
          <span class="old">￥{{item.oldPrice}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.lave}}件</span>
        </p>
      </div>
    </div>
    <mt-button type="danger" size="large" @click="getMore" v-if="flag">加载更多</mt-button>
    <mt-button type="primary" size="large" v-else disabled plain>没有更多数据。。。</mt-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      page: 1,
      flag: true
    };
  },
  created() {
    this.getGoods();
  },
  methods: {
    //获取商品信息
    getGoods() {
      this.$http.get("goodsList?_limit=4&_page=" + this.page).then(res => {
        console.log(res.body);
        console.log(res.body.length);
        if (res.body.length == 0) {
          this.flag = false;
        }
        this.goodsList = this.goodsList.concat(res.body);
      });
    },
    //加载更多
    getMore() {
      this.page++;
      this.getGoods();
    },
    goDetail(id) {
      this.$router.push("/home/goodsInfo/" + id);
    }
  }
};
</script>

<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 7px;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0;
    padding: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 293px;
    img {
      width: 100%;
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
      }
    }
  }
  .noData {
    width: 100%;
    height: 30px;
    line-height: 30px;
    margin: 5px 0;
    text-align: center;
  }
}
</style>
